//
//  ApiRequest.m
//  ScoutIOS
//
//  Created by bigknife on 13-9-6.
//  Copyright (c) 2013年 bigknife. All rights reserved.
//

#import "ApiRequest.h"
#import <UIKit/UIKit.h>
//空token
#define API_NULL_TOKEN (@"")
//TRS api 版本号
#define API_VERSION (@"0.1")
//虚拟的设备标志
#define MOCK_DEVICE_ID (@"888888")

@implementation ApiRequest{
	NSDictionary *_bizDataDict;
}
@synthesize apiVersion = _apiVersion;
@synthesize os = _os;
@synthesize appVersion = _appVersion;
@synthesize deviceId = _deviceId;
@synthesize token = _token;
@synthesize latitude = _latitude;
@synthesize longitude = _longitude;
@synthesize requestTime = _requestTime;
@synthesize requestId = _requestId;
@synthesize sign = _sign;

@synthesize bizDataDict = _bizDataDict;

- (id)init {
	self = [super init];
	if (self) {
		self.os = [[@"ios" stringByAppendingString:@":"] stringByAppendingString:[[UIDevice currentDevice] systemVersion]];
		self.token = [Session sharedInstance].token;
		if(!self.token) self.token = API_NULL_TOKEN;
		self.appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
		self.apiVersion = API_VERSION;
		self.sign = [NSString stringWithFormat:@"%d", arc4random() % 10000];
		//deviceId 没什么太大用处，ios 同意使用一个无意义的数据
		self.deviceId = MOCK_DEVICE_ID;
		self.requestId = [self makeUUID];
		self.requestTime = [[NSNumber numberWithDouble:[[NSDate date] timeIntervalSince1970] * 1000] longValue];
	}

	return self;
}
+(id) requestWithBizPath:(NSData *)dataArray nameArray:(NSString *)nameArray fileNameArray:(NSString *)fileNameArray mimeType:(NSString *)mimeType{
	return [[ApiRequest alloc] initWithBizPath:dataArray nameArray:nameArray fileNameArray:fileNameArray mimeType:mimeType latitude:0 longitude:0];
}

- (id)initWithBizData:(NSDictionary *)dictionary latitude:(double)latitude longitude:(double)longitude{
	
	self = [self init];
	if(self){
		self.latitude = latitude;
		self.longitude = longitude;
		self.bizDataDict = dictionary;
	}
    return self;
}
- (id)initWithBizPath:(NSData *)dataArray nameArray:(NSString *)nameArray fileNameArray:(NSString *)fileNameArray mimeType:(NSString *)mimeType latitude:(double)latitude longitude:(double)longitude{
	
	self = [self init];
	if(self){
		self.latitude = latitude;
		self.longitude = longitude;
		self.dataArray = dataArray;
		self.nameArray = nameArray;
		self.fileNameArray = fileNameArray;
		self.mimeType = mimeType;
	}
	return self;
}

+ (id)requestWithBizData:(NSDictionary *)dictionary
{
	return [[ApiRequest alloc] initWithBizData:dictionary latitude:0 longitude:0];
}

- (NSString *)makeUUID {
	
	CFUUIDRef uuid = CFUUIDCreate(nil);

	CFStringRef	uuidString = CFUUIDCreateString(nil, uuid);

	NSString *uuidNSString = (__bridge NSString *)uuidString;
	CFRelease(uuid);

	NSString *retString = [NSString stringWithString:uuidNSString];
	CFRelease(uuidString);

	return retString;
}
@end